"""
基础模型
包含:
1. BaseModel - 基础模型
2. SoftDeleteModel - 软删除模型
"""

from datetime import datetime
from typing import Any, Dict, List, Optional

from tortoise import fields
from tortoise.manager import Manager
from tortoise.models import Model
from tortoise.queryset import QuerySet

DEFAULT_FIELD = ("is_delete", "deleted_at", "updated_at", "created_at")


class BaseModel(Model):
    """Basic model, all models should inherit from this model."""

    id = fields.BigIntField(db_index=True, description="ID", pk=True, generated=True)
    created_at = fields.DatetimeField(db_index=True, auto_now_add=True, null=True, description="创建时间")
    updated_at = fields.DatetimeField(db_index=True, auto_now=True, null=True, description="更新时间")

    class Meta:
        abstract = True


class ValidObjectManager(Manager):
    def get_queryset(self) -> QuerySet:
        return super().get_queryset().filter(is_delete=False)


class SoftDeleteModel(BaseModel):
    """Soft delete model, all models with soft delete feature should inherit from this model."""

    is_delete = fields.BooleanField(db_index=True, default=False)
    deleted_at = fields.DatetimeField(db_index=True, null=True)

    class Meta:
        abstract = True

    objects = ValidObjectManager()

    async def soft_delete(self, **kwargs: Dict[str, Any]) -> None:
        self.is_delete = True
        self.deleted_at = datetime.now()
        await self.save(**kwargs)
